package com.hs.ding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.ding.mapper.NeedMapper;
import com.hs.ding.mapper.TransportMapper;
import com.hs.ding.mapper.VehicleMapper;
import com.hs.ding.pojo.Dispatch;
import com.hs.ding.mapper.DispatchMapper;
import com.hs.ding.pojo.Need;
import com.hs.ding.pojo.Transport;
import com.hs.ding.pojo.Vehicle;
import com.hs.ding.service.IDispatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-06-20
 */
@Service
public class DispatchServiceImpl extends ServiceImpl<DispatchMapper, Dispatch> implements IDispatchService {

    @Autowired
    private NeedMapper needMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private TransportMapper transportMapper;

    @Autowired
    private DispatchMapper dispatchMapper;


    @Override
    public ResultBean findAllDispatch(Integer currentPage, Integer pageSize,String dOrderId) {

        //查询所有状态
        QueryWrapper<Dispatch> qw = new QueryWrapper<>();

        if (StringUtils.hasText(dOrderId)) {
            qw.like("d_order_id",dOrderId);
        }

        //查询状态为0
        //qw.eq("d_status",0);

        //分页构造器
        Page<Dispatch> page = new Page<>(currentPage, pageSize);
        Page<Dispatch> p = dispatchMapper.selectPage(page, qw);

        // 分页的的记录
        List<Dispatch> records = p.getRecords();


        for (Dispatch dispatch : records) {

            Transport transport = transportMapper.selectById(dispatch.gettOrderId());

            Vehicle vehicle = vehicleMapper.selectById(transport.getvOrderId());

            Need need = needMapper.selectById(vehicle.getOrderId());

            transport.setNeed(need);

            dispatch.setTransport(transport);
        }


        Long total = p.getTotal();
        PageBean pageBean = new PageBean();
        pageBean.setList(records);
        pageBean.setTotal(total.intValue());

        ResultBean ok = new ResultBean();
        ok.setData(pageBean);

        return ok;
    }

    @Override
    public List<Dispatch> findDealDispatch() {
        //查询所有状态
        QueryWrapper<Dispatch> qw = new QueryWrapper<>();

        //查询已处理的订单，即 d_status 状态为 1
        qw.eq("d_status",1);

        List<Dispatch> dispatches = dispatchMapper.selectList(qw);

        for (Dispatch dispatch : dispatches) {

            Transport transport = transportMapper.selectById(dispatch.gettOrderId());

            Vehicle vehicle = vehicleMapper.selectById(transport.getvOrderId());

            Need need = needMapper.selectById(vehicle.getOrderId());

            transport.setNeed(need);

            dispatch.setTransport(transport);
        }

        return dispatches;
    }
}
